package com.wang.cloud.common.log.filters;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 打印接口请求耗时
 *
 * @author wangye
 * @date 2023/8/1 0001
 */
@Slf4j
@Component
public class LogFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 打印请求信息
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Long startTime = System.currentTimeMillis();
        log.info("Method={} Path={}", request.getMethod(), request.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
        log.info("Method={} Path={} time={} ms", request.getMethod(), request.getRequestURI(), System.currentTimeMillis() - startTime);
    }
}
